<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\facade\Session;

/*博客*/
class Redirect extends Base
{  
    /*列表*/
    public function index(Request $request){
       $this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];

        $data=$this->navleft($data_get);

        $this->assign($data);

        return $this->fetch();
    }

    public function get_data(Request $request){
        $this->check_session();
        $get=$request->get();
        
        $model=new \app\admin\model\Redirect();/*调用模型查询*/
        
        $model_data=$model->get_data($get['page'],$get['limit']);
        
      
        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
       
    }

     /*新增 或者 修改 页面*/
    public function change(Request $request){
        $this->check_session();
        /*左侧导航*/
        $data_get=$request->get();

        $data=$this->navleft($data_get['nav']);
        if(isset($data_get['id'])){
          $model=new \app\admin\model\Redirect();/*调用模型查询*/
          $model_data=$model->get_edit($data_get['id']);
          $data['data_list']=$model_data;
        }
        $this->assign($data);
        return $this->fetch();
    }

     /*执行 新增 或者 修改任务*/
    public function do_change(Request $request){
    $this->check_session();
        $data=$request->post();
        if(empty($data['surl'])){
          $result=['status'=>'0','msg'=>'请输入跳转前url'];
          return json($result);
        }
        if(empty($data['r_url'])){
          $result=['status'=>'0','msg'=>'请输入跳转后url'];
          return json($result);
        }
        if(empty($data['sm'])){
          $result=['status'=>'0','msg'=>'请输入备注'];
          return json($result);
        }
        if($data['surl']==$data['r_url']){
          $result=['status'=>'0','msg'=>'两个url不能一样'];
          return json($result);
        }

        $model=new \app\admin\model\Redirect();/*调用模型查询*/
        $get=$request->get();
        if(!empty($get['id'])){
          $model_data=$model->update_data($get['id'],$data['surl'],$data['r_url'],$data['sm']);
        }else{
          $model_data=$model->insert_data($data['surl'],$data['r_url'],$data['sm']);
        }
        if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
        }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
        }    
    }

   /*删除*/
    public function del(Request $request){
        $this->check_session();
        $data=$request->post();
        if(empty($data['id'])){
            $result=['status'=>'0','msg'=>'操作有误'];
            return json($result);
        }
        $model=new \app\admin\model\Redirect();/*调用模型查询*/
        $model_data=$model->delete_data($data['id']);
        if($model_data!=1){
            $result=['status'=>'0','msg'=>'删除失败'];
            return json($result);
        }else{
            $result=['status'=>'1','msg'=>'删除成功'];
            return json($result);
        }
    }  
}